library(RCurl)
library(rjson)
library(ggmap)
library(dplyr) #数据预处理
library(maptools) #用于读取地图矢量数据
require(rgdal) #读取地图数据
library(ggplot2) #绘制地图
library(grid) #图形嵌套
library(plyr)
library(sf)
library(readr)
library(lubridate) #处理日期时间相关的R包
library(ggthemes)
library(forcats)
## 获取并整理疫情数据
# 中国及广东省疫情数据整理
url <- getURL("https://raw.githubusercontent.com/canghailan/Wuhan-2019-nCoV/master/Wuhan-2019-nCoV.csv")
data1 <- read.csv(text = url, check.names = F)
url3 <- getURL("https://raw.githubusercontent.com/sculjc/sculjc.github.io/master/China_province.csv")
China_province <-read.csv(text = url3, check.names = F)
Guangdong_data0 <- data1[data1[,4]=="广东省",]
Guangdong <- Guangdong_data0[Guangdong_data0[,6]=="",]
Guangdong2 <- Guangdong_data0[Guangdong_data0[,6]!="",]
Guangdong2 <- Guangdong2[Guangdong2[,6]!="来源未公布",]
Guangdong2 <- Guangdong2[Guangdong2[,6]!="境外输入",]
Guangdong2 <- Guangdong2[Guangdong2[,6]!="未公布来源",]

## 最新更新日期:latest_day
latest_day <- Guangdong[end(Guangdong[,1]),1]
# 中国数据整理
China_all <- data1[data1$country == "中国",]
China_daily <- China_all[China_all$province == "",]
China_confirmed <- China_daily$confirmed[end(China_daily$confirmed)]
China_cured_rate0 <- China_daily$cured[end(China_daily$cured)]/China_daily$confirmed[end(China_daily$confirmed)]
China_cured_rate <- paste(round(China_cured_rate0*100, 3), "%")
China_dead_rate0 <- China_daily$dead[end(China_daily$dead)]/China_daily$confirmed[end(China_daily$confirmed)]
China_dead_rate <- paste(round(China_dead_rate0*100, 3), "%")
province_all <- China_all[China_all$province != "",]
province_all_latest <- province_all[province_all$date == latest_day[1],]
province_each_latest <- province_all_latest[province_all_latest$city == "",]
province_each_latest$dead_rate <- 100*province_each_latest$dead/province_each_latest$confirmed
province_each_latest$cured_rate <- 100*province_each_latest$cured/province_each_latest$confirmed
China_province_each_latest <- merge.data.frame(province_each_latest, China_province, by.x = "provinceCode", by.y = "province_code")
## 广东地图：Guangdong_map
json_data <- fromJSON(file = "https://geo.datav.aliyun.com/areas_v2/bound/440000_full.json")
draw_data <- data.frame()
for(i in 1:length(json_data[['features']])) {
    district <- json_data[['features']][[i]][['properties']][['name']]
    for(j in 1:length(json_data[['features']][[i]][['geometry']][['coordinates']])) {
        df_temp_j <- t(as.data.frame(json_data[['features']][[i]][['geometry']][['coordinates']][[j]]))
        df_temp_j <- as.data.frame(df_temp_j)
        names(df_temp_j)[1:2] <- c('lon','lat')
        df_temp_j[['district']] <- rep(district,length(json_data[['features']][[i]][['geometry']][['coordinates']][[j]]))
        df_temp_j[['id']] <- rep(j,length(json_data[['features']][[i]][['geometry']][['coordinates']][[j]]))
        draw_data <- rbind(draw_data,df_temp_j)
    }
}
row.names(draw_data)<-NULL
select1 <- draw_data[,3]!="东沙群岛"
Guangdong_map <- draw_data[select1,]

## 广东人口：Guangdong_pop
url2 <- getURL("https://raw.githubusercontent.com/sculjc/sculjc.github.io/master/2018population_guangdong.csv")
Guangdong_pop <- read.csv(text = url2, check.names = F)


## 各地级市最新日期数据:Guangdong_latest_covid19_map
Guangdong_latest0 <- Guangdong_data0[Guangdong_data0[,1]==latest_day[1],]
Guangdong_latest_covid19 <- Guangdong_latest0[-(1:2),]
Guangdong_latest_covid19_pop <- merge.data.frame(Guangdong_latest_covid19, Guangdong_pop, by.x = "city", by.y = "city_name")
# confirmed_rate
Guangdong_latest_covid19_pop$confirmed_rate <- (Guangdong_latest_covid19_pop$confirmed)/(Guangdong_latest_covid19_pop$people_num/10)
# dead_rate
Guangdong_latest_covid19_pop$dead_rate <- (Guangdong_latest_covid19_pop$dead)/(Guangdong_latest_covid19_pop$confirmed)
# cured_rate
Guangdong_latest_covid19_pop$cured_rate <- (Guangdong_latest_covid19_pop$cured)/(Guangdong_latest_covid19_pop$confirmed)
Guangdong_latest_covid19_pop[is.na(Guangdong_latest_covid19_pop)] <- 0
Guangdong_latest_covid19_pop$cured_rate[Guangdong_latest_covid19_pop$English_name=="Yunfu"] <- 1
# 合并数据
Guangdong_latest_covid19_map <- merge.data.frame(Guangdong_map, Guangdong_latest_covid19_pop, by.x = "district", by.y = "city")

## 排序
# 确诊城市排序：confirmed_city_order
confirmed_order <- order(Guangdong_latest_covid19_pop$confirmed, decreasing=T)
confirmed_order_count <- sort(Guangdong_latest_covid19_pop$confirmed, decreasing=T)
#city_A <- Guangdong_latest_covid19_pop$English_name[Guangdong_latest_covid19_pop$confirmed == confirmed_order[1]]
#city_B <- Guangdong_latest_covid19_pop$English_name[confirmed_order[2]]
#city_C <- Guangdong_latest_covid19_pop$English_name[confirmed_order[3]]
confirmed_city_order <- character()
for (i in 1:length(confirmed_order)) {
    confirmed_city_order[i] <- Guangdong_latest_covid19_pop$English_name[confirmed_order[i]]
}

# 按排序输出城市名与确诊数
con_city_order <- character()
for (k in 1:length(confirmed_order)) {
    temp1 <- confirmed_city_order[k]
    temp2 <- confirmed_order_count[k]
    con_city_order <- paste(con_city_order, temp1, "(", temp2, ")", ",")
}


# 发病率城市排序:confirmed_rate_city_order
confirmed_rate_order <- order(Guangdong_latest_covid19_pop$confirmed_rate, decreasing = T)
confirmed_rate_order_count <- sort(Guangdong_latest_covid19_pop$confirmed_rate, decreasing=T)
#city_A1 <- Guangdong_latest_covid19_pop$English_name[confirmed_rate_order[1]]
#city_B1 <- Guangdong_latest_covid19_pop$English_name[confirmed_rate_order[2]]
#city_C1 <- Guangdong_latest_covid19_pop$English_name[confirmed_rate_order[3]]
confirmed_rate_city_order <- character()
for (j in 1:length(confirmed_rate_order)) {
    confirmed_rate_city_order[j] <- Guangdong_latest_covid19_pop$English_name[confirmed_rate_order[j]]
}

# 按城市输出城市名与发病率
con_rate_city_order <- character()
for (i in 1:length(confirmed_rate_order)) {
    temp1 <- confirmed_rate_city_order[i]
    temp2 <- round(confirmed_rate_order_count[i], 3)
    con_rate_city_order <- paste(con_rate_city_order, temp1, "(", temp2, ")", ",")
}
# 死亡人数城市排序:dead_city_order
city_dead <- Guangdong_latest_covid19_pop[Guangdong_latest_covid19_pop$dead != 0,]
dead_city_order <- order(city_dead$dead, decreasing = T)
dead_city_order_count <- sort(city_dead$dead, decreasing=T)
dead_city_order_name <- character()
for (j in 1:length(confirmed_rate_order)) {
    dead_city_order_name[j] <- city_dead$English_name[dead_city_order[j]]
}

# 按城市输出城市名与死亡人数
death_city_order <- character()
for (i in 1:length(dead_city_order)) {
    temp1 <- dead_city_order_name[i]
    temp2 <- dead_city_order_count[i]
    death_city_order <- paste(death_city_order, temp1, "(", temp2, ")", ",")
}

# 治愈城市排序：confirmed_city_order
cured_order <- order(Guangdong_latest_covid19_pop$cured, decreasing=T)
cored_order_count <- sort(Guangdong_latest_covid19_pop$cured, decreasing=T)
cured_city_order <- character()
for (i in 1:length(cured_order)) {
    cured_city_order[i] <- Guangdong_latest_covid19_pop$English_name[cured_order[i]]
}

# 按排序输出城市名与治愈数
cured_city_name_order <- character()
for (k in 1:length(cured_order)) {
    temp1 <- cured_city_order[k]
    temp2 <- cored_order_count[k]
    cured_city_name_order <- paste(cured_city_name_order, temp1, "(", temp2, ")", ",")
}
# 广东省数据：Guangdong
Guangdong$population <- 11346
Guangdong$confirmed_rate <- (Guangdong$confirmed)/(Guangdong$population/10)
Guangdong$cured_rate <- Guangdong$cured/Guangdong$confirmed
Guangdong$dead_rate <- Guangdong$dead/Guangdong$confirmed
Guangdong_lj_confirmed <- Guangdong$confirmed[end(Guangdong$confirmed)]
Guangdong_lj_confirmed_rate <- Guangdong$confirmed_rate[end(Guangdong$confirmed_rate)]
Guangdong_lj_dead <- Guangdong$dead[end(Guangdong$dead)]
Guagdong_lj_dead_rate <- Guangdong$dead_rate[end(Guangdong$dead_rate)]
Guangdong_lj_cured <- Guangdong$cured[end(Guangdong$cured)]
Guangdong_lj_cured_rate <- Guangdong$cured_rate[end(Guangdong$cured_rate)]

Guagdong_cured_rate <- paste(round(Guangdong_lj_cured_rate[1]*100, 3),"%")  # %表示治愈率
Guangdong_death_rate <- paste(round(Guagdong_lj_dead_rate[1]*100, 3), "%")  # %表示病死率
## 各地级市数据:Guangdong_data
Guangdong_data <-merge.data.frame(Guangdong2, Guangdong_pop, by.x = "city", by.y = "city_name")
# confirmed_rate
Guangdong_data$confirmed_rate <- (Guangdong_data$confirmed)/(Guangdong_data$people_num/10)
# dead_rate
Guangdong_data$dead_rate <- (Guangdong_data$dead)/(Guangdong_data$confirmed)
# cured_rate
Guangdong_data$cured_rate <- (Guangdong_data$cured)/(Guangdong_data$confirmed)
Guangdong_data$City <- Guangdong_data$English_name
guangzhou <- Guangdong_data[Guangdong_data[,7]==440100,]
shaoguan <- Guangdong_data[Guangdong_data[,7]==440200,]
shenzhen <- Guangdong_data[Guangdong_data[,7]==440300,]
zhuhai <- Guangdong_data[Guangdong_data[,7]==440400,]
shantou <- Guangdong_data[Guangdong_data[,7]==440500,]
foshan <- Guangdong_data[Guangdong_data[,7]==440600,]
jiangmen <- Guangdong_data[Guangdong_data[,7]==440700,]
zhanjiang <- Guangdong_data[Guangdong_data[,7]==440800,]
maoming <- Guangdong_data[Guangdong_data[,7]==440900,]
zhaoqing <- Guangdong_data[Guangdong_data[,7]==441200,]
huizhou <- Guangdong_data[Guangdong_data[,7]==441300,]
meizhou <- Guangdong_data[Guangdong_data[,7]==441400,]
shanwei <- Guangdong_data[Guangdong_data[,7]==441500,]
heyuan <- Guangdong_data[Guangdong_data[,7]==441600,]
yangjiang <- Guangdong_data[Guangdong_data[,7]==441700,]
qingyuan <- Guangdong_data[Guangdong_data[,7]==441800,]
dongguan <- Guangdong_data[Guangdong_data[,7]==441900,]
zhongshan <- Guangdong_data[Guangdong_data[,7]==442000,]
chaozhou <- Guangdong_data[Guangdong_data[,7]==445100,]
jieyang <- Guangdong_data[Guangdong_data[,7]==445200,]
yunfu <- Guangdong_data[Guangdong_data[,7]==445300,]

# 获取城市英文名：city_name
city_name <- Guangdong_pop$English_name

## 绘制地图颜色分级：
# 确诊患者颜色分级
ggplot()+
    geom_polygon(data = Guangdong_latest_covid19_map,aes(x=lon,y=lat,group=interaction(district,id),fill=confirmed), col="#3a423d")+
    scale_fill_gradient(low="#faf1c5",high="#ff6200")+
    theme_minimal()+
    xlab(NULL) + ylab(NULL)+
    theme_bw()+
    scale_y_continuous(breaks= NULL)+
    scale_x_continuous(breaks= NULL)+
    theme(legend.position=c(.85, .2))

# 发病率颜色分级
ggplot()+
    geom_polygon(data = Guangdong_latest_covid19_map,aes(x=lon,y=lat,group=interaction(district,id),fill=confirmed_rate), col="#3a423d")+
    scale_fill_gradient(low="#faf1c5",high="#ff6200")+
    theme_minimal()+
    xlab(NULL) + ylab(NULL)+
    theme_bw()+
    scale_y_continuous(breaks= NULL)+
    scale_x_continuous(breaks= NULL)+
    theme(legend.position=c(.85, .2))

## 绘制各城市确诊病例曲线图
ggplot()+
    geom_line(data = guangzhou,aes(x = date,y = confirmed,colour = City,group = 1),size=1)+
    geom_line(data = shaoguan,aes(x = date,y = confirmed,colour = City,group = 1),size=1)+
    geom_line(data = shenzhen,aes(x = date,y = confirmed,colour = City,group = 1),size=1)+
    geom_line(data = zhuhai,aes(x = date,y = confirmed,colour = City,group = 1),size=1)+
    geom_line(data = shantou,aes(x = date,y = confirmed,colour = City,group = 1),size=1)+
    geom_line(data = foshan,aes(x = date,y = confirmed,colour = City,group = 1),size=1)+
    geom_line(data = jiangmen,aes(x = date,y = confirmed,colour = City,group = 1),size=1)+
    geom_line(data = zhanjiang,aes(x = date,y = confirmed,colour = City,group = 1),size=1)+
    geom_line(data = maoming,aes(x = date,y = confirmed,colour = City,group = 1),size=1)+
    geom_line(data = zhaoqing,aes(x = date,y = confirmed,colour = City,group = 1),size=1)+
    geom_line(data = huizhou,aes(x = date,y = confirmed,colour = City,group = 1),size=1)+
    geom_line(data = meizhou,aes(x = date,y = confirmed,colour = City,group = 1),size=1)+
    geom_line(data = shanwei,aes(x = date,y = confirmed,colour = City,group = 1),size=1)+
    geom_line(data = heyuan,aes(x = date,y = confirmed,colour = City,group = 1),size=1)+
    geom_line(data = yangjiang,aes(x = date,y = confirmed,colour = City,group = 1),size=1)+
    geom_line(data = qingyuan,aes(x = date,y = confirmed,colour = City,group = 1),size=1)+
    geom_line(data = dongguan,aes(x = date,y = confirmed,colour = City,group = 1),size=1)+
    geom_line(data = zhongshan,aes(x = date,y = confirmed,colour = City,group = 1),size=1)+
    geom_line(data = chaozhou,aes(x = date,y = confirmed,colour = City,group = 1),size=1)+
    geom_line(data = jieyang,aes(x = date,y = confirmed,colour = City,group = 1),size=1)+
    geom_line(data = yunfu,aes(x = date,y = confirmed,colour = City,group = 1),size=1)+
    scale_x_discrete(breaks = c("2020-01-15", "2020-02-15", "2020-03-15", "2020-04-15", "2020-05-15", "2020-06-15", "2020-07-15", "2020-08-15"))+
    theme_bw() #

## 各省死亡率条形图
China_province_each_latest %>%
    mutate(name = fct_reorder(China_province_each_latest$Province, China_province_each_latest$dead_rate)) %>%
    ggplot(aes(x=name, y=China_province_each_latest$dead_rate)) +
    geom_bar(stat="identity", fill="#3b3c3d", alpha=.4, width=.6) +
    coord_flip() +
    xlab("") +
    ylab("Cumulative mortality rate(%)")+
    theme_bw()
## 各省治愈率条形图
China_province_each_latest %>%
    mutate(name = fct_reorder(China_province_each_latest$Province, China_province_each_latest$cured_rate)) %>%
    ggplot(aes(x=name, y=China_province_each_latest$cured_rate)) +
    geom_bar(stat="identity", fill="#60abf6", alpha=.4, width=.6) +
    coord_flip() +
    xlab("") +
    ylab("Cumulative cure rate")+
    theme_bw()

